<template>
  <header class="header">
    <h1>todos</h1>
    <input id="toggle-all" class="toggle-all" type="checkbox" v-model="isAll" />
    <label for="toggle-all"></label>
    <input
      class="new-todo"
      placeholder="输入任务名称-回车确认"
      autofocus
      v-model="task"
      @keydown.enter="down"
    />
  </header>
</template>

<script>
import { mapState, mapMutations } from "vuex";
export default {
  data() {
    return {
      task: "",
    };
  },
  methods: {
    ...mapMutations(["add"]),
    down() {
      this.add(this.task);
      this.task = "";
    },
  },
  computed: {
    ...mapState(["list"]),
    isAll: {
      set(val) {
        this.list.forEach((item) => (item.isDone = val));
      },
      get() {
        return this.list.every((item) => item.isDone);
      },
    },
  },
  // watch: {
  //   list: {
  //     deep: true,
  //     handler() {
  //       // console.log(val);
  //       localStorage.setItem("list", this.list);
  //     },
  //   },
  // },
};
</script>